<!DOCTYPE html>
<html>
<head>
    <title>生成请求结构</title>
</head>
<body>

<textarea id="data" cols="100" rows="10">name,age,sex</textarea>
<br>
<input type="button" name="gen" value="gen" onclick="gen()">
<br>
<textarea id="rs" cols="100" rows="20"></textarea>
<br>

<script type="text/javascript">

String.prototype.transform = function(){ 
    var re=/_(\w)/g; 
    return this.replace(re,function(){ 
        var args=arguments; 
        return args[1].toUpperCase(); 
    }) 
} 

function firstUpperCase(str) {
  return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
}

function gen(){
    var data = document.getElementById('data').value;
    fields = data.split(',')
    dump(fields)

    var rs=[];

    for (var i =  0; i < fields.length; i++) {
        var field = fields[i]
        var row = [];
        row.push(firstUpperCase(field.transform()));
        row.push('  ');
        row.push('string');
        row.push('  ');
        row.push('`');
        row.push('json:"'+ field +'"');

        row.push(' ');
        row.push('xml:"'+ field +'"');

        row.push(' ');
        row.push('form:"'+ field +'"');

        row.push(' ');
        row.push('query:"'+ field +'"');
        row.push('`');
       
      rs.push(row.join(''));
      rs.push("\n");

    }
  
  dump(rs.join(''));
  document.getElementById('rs').value = rs.join('');

}

function dump(data){
    console.log(data);
}




</script>

</body>
</html>